/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.demo.provider;

import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.demo.MyDemoService;
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class Application {
    public static void main(String[] args) throws Exception {
//        if (isClassic(args)) {
//            startWithExport();
//        } else {
            startWithBootstrap();
//        }
    }

    private static boolean isClassic(String[] args) {
        return args.length > 0 && "classic".equalsIgnoreCase(args[0]);
    }

    private static void startWithBootstrap() {
        ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
        service.setId("service");
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setGroup("groupDemo");
        service.setVersion("1.0.0");


        ServiceConfig<MyDemoServiceImpl> myDemoServiceServiceConfig = new ServiceConfig<>();
        myDemoServiceServiceConfig.setId("myDemoServiceServiceConfig");
        myDemoServiceServiceConfig.setInterface(MyDemoService.class);
        myDemoServiceServiceConfig.setRef(new MyDemoServiceImpl());

        List<ServiceConfig> serviceConfigs = new ArrayList<>();
        serviceConfigs.add(service);
        serviceConfigs.add(myDemoServiceServiceConfig);

        ApplicationConfig ac = new ApplicationConfig();
        ac.setName("dubbo-demo-api-provider");



//        ConfigCenterConfig centerConfig = new ConfigCenterConfig();
//        centerConfig.setAddress("nacos://192.168.50.227:8848");
//        centerConfig.setGroup("DUBBO_GROUP");
//        centerConfig.setNamespace("DUBBO_GROUP");

        MetadataReportConfig mrc = new MetadataReportConfig();
        mrc.setAddress("nacos://192.168.50.227:8848");


        RegistryConfig rc = new RegistryConfig();
        rc.setAddress("nacos://192.168.50.227:8849");
        rc.setGroup("DUBBO_GROUP");
        rc.setZone("Beijing");

        ProtocolConfig protocolConfig = new ProtocolConfig();
//        protocolConfig.setName("hessian");
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(-1);
//        protocolConfig.setSerialization("hessian2");
        protocolConfig.setSerialization("native-hessian");


        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider"))
                .registry(rc)
                .protocol(protocolConfig)
//                .configCenter(centerConfig)
//                .metadataReport(mrc)
                .service(service).service(myDemoServiceServiceConfig)
                .start();
//                .await();
        System.out.println("---------->>>>>");
        System.out.println("---------->>>>>");
        ConfigManager configManager = ApplicationModel.getConfigManager();
        Environment environment = ApplicationModel.getEnvironment();
        System.out.println("---------->>>>>");
        System.out.println("---------->>>>>");
        bootstrap.await();
    }

    private static void startWithExport() throws InterruptedException {
        ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
        service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        service.export();

        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }

    public <K , V> boolean test(K k, V v){
        return k.equals(v);
    }

    private static String s;

    public void test2(){
        s = test3();
    }

    public String test3(){
        String str = new String("java");
        return str;
    }

}
